package com.sso.controller;

import com.alibaba.fastjson.JSON;
import com.common.entity.Client;
import com.common.untils.CommonResult;

import com.sso.openFeign.BusinessOpenFeign;
import com.sso.token.HomeToken;
import com.sso.vo.HomeLoginVo;
import com.sso.vo.Loginvo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @create 2022-09-06
 */
@RestController
@RequestMapping("sso")
public class HomeLoginController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private BusinessOpenFeign businessOpenFeign;
    @RequestMapping("homeLogin")
    public CommonResult login(@RequestBody HomeLoginVo loginvo){
        Subject subject = SecurityUtils.getSubject();
        System.out.println("==========="+loginvo);
        try {
//            UsernamePasswordToken token = new UsernamePasswordToken(loginvo.getUsername(), loginvo.getPassword());
            HomeToken token = new HomeToken(loginvo.getMobile(),loginvo.getQianPassword());
            //交于shiro自定义realm中的认证方法
            subject.login(token);
            //生成一个唯一token 保存到redis中
            String uuid = UUID.randomUUID().toString().replace("-","");
            //登陆成功后的用户信息
            Client client = (Client) subject.getPrincipal();
            Map<String,Object> map=new HashMap<>();
            //value中应该包含用户信息    以及用户具有的权限
            if (!client.getBid().equals(0)){
                map.put("isTeacher",1);
            }else {
                System.out.println("-----------");
                map.put("isTeacher",0);
            }
            map.put("mobile",client.getMobile());
            map.put("memberId",client.getId());
            map.put("client",client);
            map.put("homeToken",uuid);
            redisTemplate.opsForValue().set(uuid, JSON.toJSONString(map),24, TimeUnit.HOURS);
            return new CommonResult(2000,"登陆成功",map);
        }catch (Exception e){
            e.printStackTrace();
            return  new CommonResult(5000,"认证失败",null);
        }
    }
    @PostMapping("homeTuichuLogin")
    public CommonResult tuichulogin(HttpServletRequest request){
        String token = request.getHeader("homeToken");
        Boolean delete = redisTemplate.delete(token);
        if (delete){
            return new CommonResult(2000,"退出登录成功",null);
        }
        return new CommonResult(5000,"退出登录失败",null);

    }

}
